import React from 'react';
import { Form, Row, Col } from 'antd';
import useSalaryData from '@/components/SalaryRefTable/useSalaryData';

import { ScaleBar } from './index';

// 每一个分位段，比例关系不一致
const getPercent = (value, [q25, q50, q75]) => {
    let result = 0;
    if (value <= q25) {
        result = (value / q25) * 25;
    } else if (value <= q50) {
        result = ((value - q25) / (q50 - q25)) * 25 + 25;
    } else if (value <= q75) {
        result = ((value - q50) / (q75 - q50)) * 25 + 50;
    } else {
        // 大于75分位时，实际是不精确的
        result = ((value - q75) / (q75 - q50)) * 25 + 75;
    }
    if (result > 100) result = 100;
    return parseInt(result, 10);
};

// 月薪->用工成本
const getCost = monthSalary => monthSalary * 1.6 * 12 + monthSalary * 4 + 8000 + 300 * 12;

/**
 * 生成图表所需数据
 * @param {*} currentSalaryCurrencyCode 原始薪资 货币单位
 * @param {*} employSalaryCurrencyCode 建议薪资 货币单位
 * @param {*} expectCurrencyCode  期望薪资 货币单位
 */
export const formatSource = ({ inputSalary = [], benchmark, type = 'salary', inputUnit = [], isCurrent = false }) => {
    const [origin = {}, suggest = {}, expect = {}] = inputSalary;
    const [currentSalaryCurrencyCode, employSalaryCurrencyCode, expectCurrencyCode] = inputUnit;
    const isSalary = type === 'salary';

    const formatAmount = (amount, unit = '', condition = isSalary) => {
        if (amount === undefined) return '--';
        const nextAmount = condition ? amount : Math.round(getCost(amount));
        return `${nextAmount || 0} ${unit}`;
    };

    if (isCurrent) {
        return [
            {
                title: '当前年薪',
                color: 'rgba(0, 0, 0, 0.02)',
                value: getPercent(origin.year, benchmark),
                fontColor: 'rgba(0, 0, 0, 0.45)',
                scaleColor: 'rgba(0, 0, 0, 0.45)',
                num: Math.round(origin.year),
                numWithCurrencyUnit: formatAmount(origin.year, currentSalaryCurrencyCode, true),
                subNum: formatAmount(origin.month),
                subNumWithCurrencyUnit: formatAmount(origin.month, currentSalaryCurrencyCode),
                subTitle: '当前月薪'
            }
        ];
    }

    return [
        {
            title: '原年薪',
            color: 'rgba(0, 0, 0, 0.02)',
            value: getPercent(origin.year, benchmark),
            fontColor: 'rgba(0, 0, 0, 0.45)',
            scaleColor: 'rgba(0, 0, 0, 0.45)',
            num: origin.year,
            numWithCurrencyUnit: formatAmount(origin.year, currentSalaryCurrencyCode, true),
            subNum: formatAmount(origin.month),
            subNumWithCurrencyUnit: formatAmount(origin.month, currentSalaryCurrencyCode),
            subTitle: isSalary ? '原月薪' : '原成本'
        },
        {
            title: '建议年薪',
            color: '#5590F6',
            value: getPercent(suggest.year, benchmark),
            fontColor: '#fff',
            num: suggest.year,
            numWithCurrencyUnit: formatAmount(suggest.year, employSalaryCurrencyCode, true),
            subNum: formatAmount(suggest.month),
            subNumWithCurrencyUnit: formatAmount(suggest.month, employSalaryCurrencyCode),
            subTitle: isSalary ? '建议月薪' : '建议成本'
        },
        {
            title: '期望年薪',
            color: '#FFA22D',
            value: getPercent(expect.year, benchmark),
            fontColor: '#fff',
            num: expect.year,
            numWithCurrencyUnit: formatAmount(expect.year, expectCurrencyCode, true),
            subNum: formatAmount(expect.month),
            subNumWithCurrencyUnit: formatAmount(expect.month, expectCurrencyCode),
            subTitle: isSalary ? '期望月薪' : '期望成本'
        }
    ].map(item => ({ ...item, num: Math.round(item.num), subNum: Math.round(item.subNum) }));
};

const DiffSalary = React.forwardRef(({ oneLineLayout, commInfo, talentInfo, form }, ref) => {
    const { selectSalaryData: salaryData } = useSalaryData({
        positionCode: form?.getFieldValue('positionCode') ?? talentInfo.positionCode,
        cityDictValue: talentInfo.companyAreaCode
    });
    // 醉了
    const origin = {
        month:
            form?.getFieldValue('salarySaveDTO.monthSalary') ??
            commInfo?.recruitSalaryRecordVO?.monthSalary ??
            talentInfo?.currentMonthSalary,
        year: form?.getFieldValue('salarySaveDTO.yearSalary') ?? commInfo?.recruitSalaryRecordVO?.yearSalary
    };

    const suggest = {
        month:
            form?.getFieldValue('suggestedMonthSalary') ??
            form?.getFieldValue('salarySaveDTO.suggestedMonthSalary') ??
            commInfo?.recruitEmployMoreCommunicateSalaryVO?.newSuggestedMonthSalary ??
            commInfo?.recruitSalaryRecordVO?.suggestedMonthSalary,
        year:
            form?.getFieldValue('suggestedYearSalary') ??
            form?.getFieldValue('salarySaveDTO.suggestedYearSalary') ??
            commInfo?.recruitEmployMoreCommunicateSalaryVO?.newSuggestedYearSalary ??
            commInfo?.recruitSalaryRecordVO?.suggestedYearSalary
    };

    const expect = {
        month:
            form?.getFieldValue('salarySaveDTO.expectMonthSalary') ??
            commInfo?.recruitSalaryRecordVO?.expectMonthSalary,
        year: form?.getFieldValue('salarySaveDTO.expectYearSalary') ?? commInfo?.recruitSalaryRecordVO?.expectYearSalary
    };

    const { q25, q50, q75 } = salaryData;

    const benchmark = [q25, q50, q75];
    const inputSalary = [origin, suggest, expect];

    React.useImperativeHandle(ref, () => ({
        getPercent: () => getPercent(suggest.year, benchmark)
    }));

    const inputUnit = [
        commInfo?.recruitSalaryRecordVO?.currentSalaryCurrencyCode || // HR在薪资沟通页面设置的薪资货币单位
        talentInfo?.currentSalaryCurrencyCode || // 用户在邮件中填写的原始薪资货币单位
            talentInfo?.companyCurrencyCode, // 行政机构用于兜底显示
        talentInfo?.employSalaryCurrencyCode || talentInfo?.companyCurrencyCode,
        talentInfo?.expectSalaryCurrencyCode || talentInfo?.companyCurrencyCode
    ];

    return (
        <Row>
            <Col span={24}>
                <Form.Item label="薪资对比" {...oneLineLayout}>
                    <ScaleBar sourceData={formatSource({ inputSalary, benchmark, type: 'salary', inputUnit })} />
                </Form.Item>
            </Col>
            <Col span={24}>
                <Form.Item label="用工成本对比" {...oneLineLayout}>
                    <ScaleBar
                        sourceData={formatSource({ inputSalary, benchmark, type: 'cost', inputUnit })}
                        titleKey="subTitle"
                    />
                </Form.Item>
            </Col>
        </Row>
    );
});

DiffSalary.displayName = 'DiffSalary';

export default DiffSalary;
